const app = getApp();
Page({
  data: {
    typePicker: [],
    typeIndex: 0,
    name: '',
  },
  onLoad() {
    this.getGarbageClassfy()
  },
  PickerChange(e) {
    this.setData({
      typeIndex: e.detail.value
    })
  },
  addGarbageAdmin(e) {
    let {
      name
    } = e.detail.value
    if (!name) {
      return app.$util.errorToShow("请输入垃圾名称")
    }
    console.log(this.data.typeIndex)
    wx.showLoading({
      title: '提交中',
    })
    wx.cloud.callFunction({
        name: 'db',
        data: {
          $url: 'addGarbageAdmin',
          class_id: this.data.typePicker[this.data.typeIndex]._id,
          name,
        }
      }).then(res => {
        wx.hideLoading()
        app.$util.successToShow("提交成功", () => {
          app.$util.navigateBack()
          this.setData({
            typeIndex: 0,
            name: '',
            class_id: ''
          })
        })
      })
      .catch(error => {
        console.log(error)
      })
  },
  getGarbageClassfy() {
    wx.cloud.callFunction({
      name: "db",
      data: {
        $url: "getGarbageClassfyAdminList",
        page: 1,
        limit: 100
      }
    }).then(res => {
      let result = res.result
      if (result.code) {
        this.setData({
          typePicker: result.data
        })
      }
    })
  }
})